package com.siemens.industrial.web.mvc.controller.partner.user;

import com.siemens.industrial.model.partner.User;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.web.mvc.service.AuthService;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import com.siemens.industrial.web.mvc.service.partner.PartnerShiftService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.tags.form.LabelTag;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/11/15.
 */
@Controller
public class PartnerShiftController {

    @Resource
    PartnerShiftService partnerShiftService;

    @Resource
    TenantService tenantService;

    @Resource
    AuthService authService;

    @RequestMapping(value = "/partner/{tenant}/shift/list", method = RequestMethod.GET)
    public String getShiftListPage(@PathVariable("tenant") String tenantKey, String deviceId, ModelMap modelMap) {

        modelMap.put("deviceId", deviceId);
        return "partner/user/shift_list";
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/shift/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getShiftList(@PathVariable("tenant") String tenantKey, @RequestBody Map<String, Object> data) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        User user = (User) authService.currentUser();
        try {
            partnerShiftService.saveDeviceShift(tenantId, user, data);
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        } catch (ParseException e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/shift/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> checkDateIsExist(@PathVariable("tenant") String tenantKey,
                                                @RequestParam(name = "deviceId") String deviceId) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> list = partnerShiftService.shiftList(tenantId);

        return RestResponse.create(GlobalResponseCode.SUCCESS)
                .putData(list)
                .build();
    }
}
